//JMSClient.java ----
// History: 2009/12/03 - Create
package oribe.sample.platform.communicate;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import oribe.sample.app.data.StringMessage;
import oribe.sample.platform.OribeMessage;

/**
 * 
 * 
 * @author akiba
 */
public class JMSClient
{
    private QueueConnection connection_;
    private QueueSession session_;
    private Queue queue_;
    private QueueSender sender_;
    private QueueReceiver receiver_;

    public JMSClient()
    {

    }

    public void setup()
    {
        QueueConnectionFactory qcf = new ActiveMQConnectionFactory(
                ActiveMQConnection.DEFAULT_BROKER_URL);
        try
        {
            this.connection_ = qcf.createQueueConnection();
            this.session_ = this.connection_.createQueueSession(false,
                                                                QueueSession.AUTO_ACKNOWLEDGE);
            this.queue_ = this.session_.createQueue("test");
            this.sender_ = this.session_.createSender(this.queue_);
            this.receiver_ = this.session_.createReceiver(this.queue_);
        }
        catch (JMSException exception)
        {
            // TODO Auto-generated catch block
            exception.printStackTrace();
        }
    }

    public void shutdown()
    {
        try
        {
            this.receiver_.close();
        }
        catch (JMSException exception)
        {
            // TODO Auto-generated catch block
            exception.printStackTrace();
        }
        
        try
        {
            this.sender_.close();
        }
        catch (JMSException exception)
        {
            // TODO Auto-generated catch block
            exception.printStackTrace();
        }
        
        try
        {
            this.session_.close();
        }
        catch (JMSException exception)
        {
            // TODO Auto-generated catch block
            exception.printStackTrace();
        }

        try
        {
            this.connection_.close();
        }
        catch (JMSException exception)
        {
            // TODO Auto-generated catch block
            exception.printStackTrace();
        }
    }

    public void sendMessage(OribeMessage msg) throws JMSException
    {
        MapMessage jmsMessage = this.session_.createMapMessage();
        jmsMessage.setObject("a-property", msg);
        
        this.sender_.send(jmsMessage);
    }
    
    public OribeMessage receiveMessage() throws JMSException
    {
        Message jmsMessage = this.receiver_.receive();
        OribeMessage msg = translate(jmsMessage);
        
        return msg;
    }

    /**
     * @param jmsMessage
     * @return
     */
    private OribeMessage translate(Message jmsMessage)
    {
        OribeMessage msg = null;
        try
        {
            msg = new StringMessage(jmsMessage.getStringProperty("a-property"));
        }
        catch (JMSException exception)
        {
            // TODO Auto-generated catch block
            exception.printStackTrace();
        }
        return msg;
    }
}
